const getMysql = require('../mysql')
const svgCaptcha = require('svg-captcha')


// pc端普通登录
const login = (req, res) => {
    let uname = req.body.uname;
    let pass = req.body.password;
    let captcha = req.body.captcha
    // console.log(uname, pass, captcha);
    // console.log('svg', req.sessionStore.captcha);

    let sql = 'select uname,password from tarvel_user where uname=? and password=password(?)'
    getMysql(sql, uname, pass).then(result => {
        if (result.length == 0) { 
            res.send({
                code: 201,
                message: '用户名或者密码错误'
            })
        } else {
            if (captcha == req.sessionStore.captcha) {
                res.send({
                    code: 200,
                    message: '登录成功！！',
                    data: result
                })
            } else {
                res.send({
                    code: 203,
                    message: '验证码错误',
                    data: result
                })
            }
        }
    }).catch(err => {
        console.log(err);
    })
}

// app端普通登录
const loginApp = (req, res) => {
    let uname = req.body.uname;
    let pass = req.body.password;

    let sql = 'select uname,password from tarvel_user where uname=? and password=password(?)'
    getMysql(sql, uname, pass).then(result => {
        if (result.length == 0) { 
            res.send({
                code: 201,
                message: '用户名或者密码错误'
            })
        } else {
            res.send({
                code: 200,
                message: '登录成功！！',
                data: result
            })
        }
    }).catch(err => {
        console.log(err);
    })
}


// svg验证码
const svg = (req, res) => {
    var captcha = svgCaptcha.create({
        size: 4,  //验证码长度
        width: 100, //svg宽度
        height: 50, //svg高度
        // background: "#eee",//svg背景色
        noise: 2, //干扰线条数
        fontSize: 50, //字体大小
        ignoreChars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxz',   //验证码字符中排除
        color: true // 验证码的字符是否有颜色，默认没有，如果设定了背景，则默认有           
    });
    req.sessionStore.captcha = captcha.text;
    res.type('svg');
    res.send(captcha.data)
    console.log('svg', req.sessionStore.captcha);
}


// qq邮箱验证登录
const emailLogin= async(req,res)=>{
    let email =  req.body.email;
    let codeid = req.body.codeid;
    let sql = 'select * from tarvel_user where email = ?'
    getMysql(sql,email).then(result=>{
        if(codeid==req.sessionStore.codeId){
            res.send({
                code: 200,
                message: '登录成功！！',
                data: result
            })
        } else {
            res.send({
                code: 203,
                message: '验证码错误'
            })
        }
    })
}


// 手机短信验证登录 req.sessionStore.iphoneId
const iphoneLogin= async(req,res)=>{
    console.log(req.body.iphone);
    let iphone =  req.body.iphone;
    let iphoneId = req.body.iphoneId;
    let sql = 'select * from tarvel_user where iphone = ?'
    getMysql(sql,iphone).then(result=>{
        if(iphoneId==req.sessionStore.iphoneId){
            res.send({
                code: 200,
                message: '登录成功！！',
                data: result
            })
        } else {
            res.send({
                code: 203,
                message: '验证码错误'
            })
        }
    })
}


module.exports = {
    login,
    loginApp,
    svg,
    emailLogin,
    iphoneLogin
}